from app.libs.error_code import Success
from app.libs.redprint import Redprint
from app.libs.token_auth import login_required
from app.models.user import UserModel
from app.validates.user import UserForm, RegisterForm
from flask import g

api = Redprint('user')


@api.route('', methods=['GET'])
@login_required
def get_user():
    uid = g.user
    user = UserModel().query.filter_by(id=uid).first_or_404()
    return Success(data=user)


@api.route('', methods=['POST'])
def register_user():
    form = RegisterForm().validate_and_api()
    UserModel.register_by_username(form.username.data, form.nickname.data, form.password.data)
    return Success()


